package com.guigui.erp.utils;

import com.guigu.erp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Lenovo
 */
@Component
public class NumberUtils {
    @Autowired
    private DesignFileService designFileService;

    @Autowired
    private DesignModuleService designModuleService;

    @Autowired
    private ManufactureDesignProcedureService manufactureDesignProcedureService;

    @Autowired
    private StorageCellService storageCellService;

    @Autowired
    private StorageGatherService storageGatherService;

    @Autowired
    private ManufactureApplyService manufactureApplyService;


    /**
     * 产品编号的生成:100+1类编号+2类编号+3类编号+6位流水号
     * @param firstKindId I级分类编号
     * @param secondKindId II级分类编号
     * @param thirdKindId III级分类编号
     * @return
     */
    public String getProductNumber(String firstKindId,
                                   String secondKindId,
                                   String thirdKindId) {
        // 拼接类型编号
        StringBuffer buffer = new StringBuffer("100");

        // 拼接各级分类编号
        buffer.append(firstKindId)
                .append(secondKindId)
                .append(thirdKindId);

        // 获取当前最大的流水号
        String flowNumber = designFileService.getMaxFlowNumber();
        int temp = Integer.parseInt(flowNumber);
        temp ++;
        // 补齐6位字符串
        String newFlowNumber = String.format("%06d", temp);
        String number = buffer.append(newFlowNumber).toString();
        return number;
    }

    /**
     * 生成各种设计单编号：
     * 物料设计单编号 200+当前日期+4位流水号
     * 工序设计单编号 201+当前日期+4位流水号
     * 生产计划编号  300+当前日期+4位流水号
     * 派工单编号   301+当前日期+4位流水号
     * 库存编号    400+当前日期+4位流水号
     * 入库单编号   401+当前日期+4位流水号
     * 出库单编号   402+当前日期+4位流水号
     * @param type 设计单类型编号
     * @return
     */
    public String getTypeNumber(String type) {
        // 拼接类型编号100
        StringBuffer buffer = new StringBuffer(type);

        // 当前日期 格式：YYYYMMDD
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String date = sdf.format(now);
        buffer.append(date);

        // 根据类型获取对应设计单的最大的设计单编号
        String designId = "";
        switch (type) {
            case "200":
                designId = designModuleService.getMaxDesignId();
                break;
            case "201":
                designId = manufactureDesignProcedureService.getMaxDesignId();
                break;
            case "400":
                designId = storageCellService.getMaxStoreId();
                break;
            case "401":
                designId = storageGatherService.getMaxGatherId();
                break;
            case "300":
                designId = manufactureApplyService.getMaxApplyId();
                break;
        }

        if(designId == null) {
            return buffer.append("0001").toString();
        }

        // 获取当前设计单编号的4位流水号  xxxYYYYMMDDxxxx
        String flowNumber = designId.substring(11);
        // 获取当前设计单编号的年份
        String year = designId.substring(3, 7);

        String number = "";
        if(year.equals(date.substring(0,4))) {
            int temp = Integer.parseInt(flowNumber);
            temp ++;
            // 补齐4位字符串
            String newFlowNumber = String.format("%04d", temp);
            number = buffer.append(newFlowNumber).toString();
        } else {
            number = buffer.append("0001").toString();
        }
        return number;
    }
}
